package com.example.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.controller.employee.req.AttendanceReq;
import com.example.dao.IEmployeeClockDao;
import com.example.entity.BO.AttendanceStatisticBO;
import com.example.entity.BO.ClockDetailBO;
import com.example.entity.BO.TimeValueBO;
import com.example.entity.EmployeeClock;
import com.example.service.IEmployeeClockService;
import com.example.utils.DateWrap;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author ChenXiaoBin
 * on 2020-08-18
 */
@Service
public class EmployeeClockServiceImpl extends ServiceImpl<IEmployeeClockDao, EmployeeClock> implements IEmployeeClockService {

    /**
     * 上班时间
     */
    public static final String WORKTIME = "09:00:00";

    /**
     * 下班时间
     */
    public static final String CLOSETIME = "17:00:00";

    /**
     * 实现用户打卡功能
     * 默认上班时间未9点
     * 1:先确定是否已经打了上班卡如果打了上班卡在判断当前时间是否已经过了上班时间 如果过了上班时间则默认打下班卡
     * 2:
     *
     * @param employeeId 员工ID
     */
    @Override
    public void clockIn(Integer employeeId) {
        //判断打卡时间是否超过上班 true 超过（迟到） false 未超过
        Boolean isLate = DateUtil.compare(DateUtil.parse(WORKTIME), new Date()) < 0;

        //判断打卡时间是否早于下班 true 早退 false 正常
        Boolean leaveEarly = DateUtil.compare(DateUtil.parse(CLOSETIME), new Date()) > 0;

        //查询员工今日上班打卡记录
        EmployeeClock workClock = baseMapper.selectClockRecord(employeeId);
        if (workClock == null) {
            //今日未打卡，创建打卡记录
            EmployeeClock entity = new EmployeeClock();
            entity.setEmployeeId(employeeId);
            entity.setBegClockTime(new Date());
            entity.setCreateTime(new Date());
            entity.setIsLate(isLate ? 1 : 0);
            baseMapper.insert(entity);
        } else {
            //已经打了上班卡，判断当时时间是否超过上班时间,不超过上班时间不做处理
            if (isLate)  {
                workClock.setEndClockTime(new Date());
                workClock.setLeaveEarly(leaveEarly ? 1 : 0);
                baseMapper.updateById(workClock);
            }
        }
    }

    /**
     * 用户指定月份考勤统计
     *
     * @param req
     * @return
     */
    @Override
    public List<AttendanceStatisticBO> attendance(AttendanceReq req) {
        List<AttendanceStatisticBO> attendances = baseMapper.attendance(req);
        for (AttendanceStatisticBO attendance : attendances) {
            String date = DateUtil.format(attendance.getLockTime(), "yyyy-MM-dd");
            ClockDetailBO clockDetailBO = baseMapper.selectClockDetail(date, req.getEmployeeId());
            attendance.setClockDetailBO(clockDetailBO);
        }
        return attendances;
    }

    /**
     * 统计近七天打卡率
     *
     * @return
     */
    @Override
    public List<TimeValueBO> getTimeVale() {
        Date begTime = DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -6));
        Date endTime = DateUtil.endOfDay(DateUtil.offsetDay(new Date(), -0));
        String sql = generateSqlData(begTime, endTime);
        List<TimeValueBO> bo = baseMapper.getTimeValue(sql);
        return bo;
    }


    /**
     * 手动拼接sql语句
     * @param startDate
     * @param endDate
     * @return
     */

    public String generateSqlData(Date startDate, Date endDate) {
        StringBuilder sql = new StringBuilder();
        int length = DateWrap.getDayGapsBetweenDates(
                DateWrap.format(endDate, "yyyy-MM-dd"), DateWrap.format(startDate, "yyyy-MM-dd"));
        for (int i = 0; i <= length; i++) {
            Date current = DateUtil.offsetDay(endDate, i - length);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd");
            String currentFormat = simpleDateFormat.format(current);
            sql.append(" UNION (SELECT '").append(currentFormat).append("', 0)");
        }
        return sql.toString();
    }






//    public static void main(String[] args) {
//
////        String dateStr1 = "09:00:00";
////        Date date1 = DateUtil.parse(dateStr1);
////        DateUtil.compare(date1, new Date());
//
////        String date =  DateUtil.formatTime(new Date());
////        DateUtil.between(date1, new Date(), DateUnit.MINUTE);
//
//
//
//        Date startDate =  DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -6));
//        Date endDate = DateUtil.endOfDay(DateUtil.offsetDay(new Date(), 0));
//
//        StringBuilder sql = new StringBuilder();
//        int length = DateWrap.getDayGapsBetweenDates(
//                DateWrap.format(endDate, "yyyy-MM-dd"), DateWrap.format(startDate, "yyyy-MM-dd"));
//        for (int i = 0; i <= length; i++) {
//            Date current = DateUtil.offsetDay(endDate, i - length);
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            String currentFormat = simpleDateFormat.format(current);
//
//            SimpleDateFormat newsim = new SimpleDateFormat("yyyy-MM-dd");
//            String newcurr = newsim.format(current);
//            sql.append(" UNION (SELECT '").append(currentFormat).append("', 0)");
//        }
//
//        System.out.println(sql.toString());
//    }
}
